package com.bawei.home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.common.entity.CommendEntity
import com.bawei.home.databinding.ItemVideoCommendBinding
import com.bawei.image.ImageOptions
import com.bawei.image.ImageStrategy
import com.bawei.image.ImgLoader
import com.bumptech.glide.Glide

class ViewCommendAdapter(private val list: MutableList<CommendEntity>): RecyclerView.Adapter<ViewCommendAdapter.ItemCommendViewHolder>() {
    private var imgLoader: ImgLoader = ImgLoader.Builder.setImageStrategy(ImageStrategy.GLIDE).build()

    //圆形图片参数
    private var imgCircleOption: ImageOptions = ImageOptions.Builder
        .setImageSize(intArrayOf(80, 80))
        .setCircleImage(true)
        .setErrorImage(com.bawei.common.R.drawable.ic_logo)
        .setPlaceImage(com.bawei.common.R.drawable.ic_logo)
        .skipMemoryCache(true)
        .build()
    inner class ItemCommendViewHolder(val dataBinding: ItemVideoCommendBinding) : RecyclerView.ViewHolder(dataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemCommendViewHolder {
        return ItemCommendViewHolder(ItemVideoCommendBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: ItemCommendViewHolder, position: Int) {
        var entity = list[position]
        var userIconImageView = holder.dataBinding.ivUserIcon
        imgLoader.loadUrlIntoImageView(holder.dataBinding.root.context, entity.userlogo, userIconImageView, imgCircleOption)
        holder.dataBinding.tvNickName.text = if (entity.nickname.isNullOrEmpty()) "爱唱歌的小孩" else entity.nickname
        holder.dataBinding.tvCommend.text = entity.content
        holder.dataBinding.tvCreateTime.text = "发布时间：${entity.createtime}"
    }

    override fun onViewRecycled(holder: ItemCommendViewHolder) {
        var userIconImage = holder.dataBinding.ivUserIcon
        Glide.with(holder.dataBinding.root.context).clear(userIconImage)
    }


}